<!--
    自定义审批意见弹框 公共插件

       参数:对象类型：
            Obj: {
                caseType: 业务类型 （枚举类型：经销商:SP ）,
                roleType: 角色类型,
            }
            customTitle:  String 弹窗、表格标题
 -->
<template>
    <div class="case-type">
        <Button type="primary" size="small" style="margin-bottom: 16px" @click="saveView">添加</Button>
        <Table border :columns="columns" :data="tableData">
            <template slot-scope="{ row }" slot="action">
                <div>
                    <Button type="primary" size="small" style="margin-right: 5px" @click="editView(row)">编辑</Button>
                    <Button type="error" size="small" @click="deleteCase(row)">删除</Button>
                </div>
            </template>
        </Table>

        <Modal :title="titleName" v-model="modalVisibleDecoration" :mask-closable='false' :width="500">
            <Form ref="formData" :model="formData" :label-width="120" inline>
                <FormItem
                    :label="customTitle"
                    prop="opinion"
                    :rules="{required: true, message: '请输入' + customTitle  , trigger: 'blur'}"
                    >
                    <Input type="text" style="width: 350px" v-model="formData.opinion"/>
                </FormItem>
            </Form>
            <div slot="footer" class="class-footer">
                <Button type="primary" :loading="saveLoading" @click="saveCase">保存</Button>
                <Button type="text" @click="cencalDecoration">关闭</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
    import {
        getCase, saveCase, editCase, deleteCase
    } from "_p/basic/api/contract/common/case-type/case-type";
    import {deepClone} from "@/libs/utils/ObjectClone";

    export default {
        props: {
            Obj: {
                //业务类型 caseType: "",
                //角色类型 roleType: "",
                caseType: Object,
                contractBusiness: Object,
                required: true
            },
            customTitle:{
                type:String,
                default:'处理意见'
            }
        },
        data() {
            return {
                formData: {
                    opinion: "",
                    contractBusiness: "",
                    caseType: ""
                },
                modalVisibleDecoration: false,
                saveLoading: false,
                operationLoading: false,
                tableData: [],
                titleName: "",
                columns: [
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        width: 300,
                        slot: 'action',
                        // fixed: 'right'
                    },
                    {
                        title: this.customTitle,
                        key: "opinion",
                    },
                ],
            }
        },
        mounted() {
            this.init();
        },
        methods: {
            init() {
                this.getCase();
            },
            getCase() {
                if (this.Obj) {
                    getCase(this.Obj.caseType,this.Obj.contractBusiness).then(res => {
                        if (res.code === "0000") {
                            this.tableData = res.data;
                        }
                    });
                }
            },
            saveCase() {
                console.log(this.formData)
                this.$refs['formData'].validate(valid => {
                    if(valid){
                        this.saveLoading = true;
                        if (Object.prototype.hasOwnProperty.call(this.formData, "id")) {
                            editCase(this.formData).then(res => {
                                if (res.code === "0000") {
                                    this.$Message.success(res.msg);
                                    this.getCase();
                                }
                                this.cb()
                            }).catch(error => {
                                console.error(error);
                                this.cb()
                                this.$Message.error("系统未知错误，请反馈给管理员");
                            });
                        } else {
                            console.log(this.formData);
                            saveCase(this.formData).then(res => {
                                if (res.code === "0000") {
                                    this.$Message.success(res.msg);
                                    this.modalVisibleDecoration = false;
                                    this.getCase();
                                }
                                this.cb()
                            }).catch(error => {
                                console.error(error);
                                this.cb()
                                this.$Message.error("系统未知错误，请反馈给管理员");
                            });
                        }
                    }
                })
            },
            cb(){
                this.saveLoading = false;
                this.modalVisibleDecoration = false;
                this.$refs.formData.resetFields();

            },
            deleteCase(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除 " + v.opinion + " ?",
                    onOk: () => {
                        this.operationLoading = true;
                        deleteCase(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.getCase();
                            }
                        }).catch(error => {
                            console.error(error);
                            this.operationLoading = false;
                            this.$Message.error("系统未知错误，请反馈给管理员");
                        });
                    }
                });
            },
            cencalDecoration() {
                this.$refs.formData.resetFields();
                this.modalVisibleDecoration = false;
            },
            saveView() {
                this.titleName = "新增" + this.customTitle;
                this.formData = this.Obj;// 5332 【撤销收款】银行代扣撤销处理-审批区域，新增审批意见，添加按钮跳转不正确
                this.modalVisibleDecoration = true;
            },
            editView(v) {
                this.titleName = "编辑" + this.customTitle;
                this.formData = deepClone(v);
                this.modalVisibleDecoration = true;
            }

        }
    }
</script>
<style scoped>
    .case-type >>> .ivu-input[disabled] {
        color: black;
    }

    .case-type >>> .ivu-select-disabled .ivu-select-selected-value {
        color: #515a6e;
    }

    .case-type >>> .ivu-input-number-input[disabled] {
        color: black;
    }
</style>
